import { Builder, By, Key } from 'selenium-webdriver'
import delay from 'delay'
import fs from 'fs'

const driver = new Builder().forBrowser('chrome').build()
driver.manage().window().maximize()

let low = 0
let mid = 0
let high = 0

async function start() {
  await driver.get('https://www.lagou.com')
  driver.manage().deleteAllCookies()
  const cookies = JSON.parse(fs.readFileSync('./cookies.json', 'utf-8'))
  cookies.forEach(async cookie => {
    await driver.manage().addCookie(cookie)
  })
  await driver.get('https://www.lagou.com')

  await driver.findElement(By.css('#cboxLoadedContent .tab.focus')).click()
  await driver.findElement(By.css('#search_input')).sendKeys('前端', Key.ENTER)

  await delay(1500)
  
  await getData()
}

start()

async function getData() {
  await delay(1500)
  const items = await driver.findElements(By.css('.item__10RTO'))
  const moneys = await Promise.all(items.map(async item => item.findElement(By.css('.money__3Lkgq')).getText()))
  console.log(moneys)
  moneys.forEach(money => {
    const salary = money.split('-')[0].slice(0, -1)
    salary < 10 && low++
    salary >= 10 && salary < 20 && mid++
    salary >= 20 && high++
  })
  console.log(low, mid, high)
}